package com.small.base;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.small.context.Context;
import com.small.handler.DispatcherHandle;
import com.small.handler.HelperLoader;
import com.small.utils.BaseUtil;

public class BaseInterceptor extends HttpServlet{
  /**
   * 序列化
   */
  private static final long serialVersionUID = -17946158959999974L;
  private static Logger logger = Logger.getLogger(BaseInterceptor.class);
  
  /** 初始化
   */
  @Override
  public void init() throws ServletException {
    
    /** 处理类初始化
     */
    HelperLoader.init();
    
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doPost(req, resp);
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
    logger.debug("BaseInterceptor开始接收请求...");
    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=UTF-8");
    String url = req.getRequestURI();
    Context context = new Context();
    try {
      if (!BaseUtil.useLoop(url)) {
        logger.debug("JframeInterface开始处理动态请求：" + url);
        new DispatcherHandle().dynamicRequest(req, resp, context);
      }else {
        logger.debug("JframeInterface开始处理静态请求：" + url);
        new DispatcherHandle().staticRequest(req, resp, context);
      }
    } catch (Exception e) {
      logger.error(e.toString());
    }
    logger.debug(url + "：请求结束...");
  }
  
}
